package com.koron.css2.contractmanage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.exception.ContractException;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.contractmanage.querybean.ContractManageQueryBean;
import com.koron.css2.contractmanage.vo.UserInfoVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 新建合同时回显用户信息
 *
 */
public class ContractGetUserInfo implements ServerInterface {

    /**
     * 合同类型  用于数据字典取值
     */
    private static final String CONTRACT_TYPE = "CONTRACT_TYPE";

    /**
     * 证件类型  用于数据字典取值
     */
    private static final String CERT_TYPE = "IDT";


    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ContractManageMapper contractManageMapper = factory.getMapper(ContractManageMapper.class);
            ContractManageQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), ContractManageQueryBean.class);
            MessageBean<UserInfoVO> messageBean = checkContractManageQueryBean(queryBean.getUserNo());

            Map<String, String> contractTypeMap = Tools.mapDicByCode(factory, CONTRACT_TYPE);
            Map<String, String> certTypeMap = Tools.mapDicByCode(factory, CERT_TYPE);

            UserInfoVO userInfoVO = contractManageMapper.getUserInfoByUserNo(queryBean.getUserNo());
            checkUserInfoEchoContractVO(userInfoVO);
            userInfoVO.setContractType(ContractTypeEnum.CONTRACT_TYPE_3.getContractType());
//            String contractNo = "";
//            do {
//                contractNo = Tools.getObjectId();
//            } while (contractManageMapper.countContractNo(contractNo) > 0);
//            userInfoVO.setContractNo(contractNo);
            if (contractTypeMap != null && contractTypeMap.containsKey(userInfoVO.getContractType())) {
                userInfoVO.setContractTypeName(contractTypeMap.get(userInfoVO.getContractType()));
            }
            if (certTypeMap != null && certTypeMap.containsKey(userInfoVO.getCertType())) {
                userInfoVO.setCertTypeName(certTypeMap.get(userInfoVO.getCertType()));
            }
            messageBean.setData(userInfoVO);
            return messageBean;
        } catch (Exception e) {
            logger.error("新建合同时查询用户信息失败: " + e.getMessage(), e);
            e.printStackTrace();
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "新建合同时查询用户信息失败: " + e.getMessage(), null);
        }
    }

    /**
     * 检查合同请求参数是否正确
     *
     * @param userNo
     * @return
     */
    public MessageBean<UserInfoVO> checkContractManageQueryBean(String userNo) {
        if (StringUtils.isBlank(userNo)) {
            throw new ContractException("参数中的户号不能为空！");
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "新建合同时查询用户信息成功", UserInfoVO.class);
    }

    /**
     * 检查结果集中是否有数据
     *
     * @param userInfoVO
     * @return
     */
    public void checkUserInfoEchoContractVO(UserInfoVO userInfoVO) {
        if (userInfoVO == null || StringUtils.isBlank(userInfoVO.getUserNo())) {
            throw new ContractException("不存在该用户信息！");
        }
    }

}
